package com.yd.controller.app;


import java.io.IOException;
import java.net.MalformedURLException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.pojo.Agency;
import com.yd.service.AgencyService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;
import com.yd.tool.UtsUuid;
/**
 * 代理商
 * @author Administrator
 *
 */

@Controller
public class AgencyControllerApp {
 
	@Resource
	private AgencyService agencyService;

	//代理商注册
	@RequestMapping(value = { "/app/agency/reg" })
	@ResponseBody
	public String app_agency_reg(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();

		String loginname = "";
		String loginpass = "";
		String loginpass2 = "";
		String invitecode = "";//邀请码-手机号
		if (UtsReg.getIsPhone(request.getParameter("loginname"))) {loginname = request.getParameter("loginname").trim();} else {jsonObject.put("msg", "手机号格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringPass(request.getParameter("loginpass"))) {loginpass = request.getParameter("loginpass").trim();} else {jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringPass(request.getParameter("loginpass2"))) {loginpass2 = request.getParameter("loginpass2").trim();} else {jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIsPhone(request.getParameter("invitecode"))) {invitecode = request.getParameter("invitecode").trim();} else {jsonObject.put("msg", "邀请码格式错误");jsonObject.put("error", "1");}

		
		if(agencyService.selectCount(new EntityWrapper<Agency>().eq("loginname", loginname))>0){
			jsonObject.put("msg", "账号已经存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!loginpass.equals(loginpass2)){
			jsonObject.put("msg", "两次密码不一致");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Agency agency = new Agency();
		agency.setPhone(loginname);
		agency.setLoginname(loginname);
		agency.setLoginpass(MD5Hash.getMd5HashPasswod(loginpass));
		if (!agencyService.insert(agency)) {
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
			
		}
		EntityWrapper<Agency> ew = new EntityWrapper<Agency>();
		if (ew.eq("invitecode", loginname) == null) {
			jsonObject.put("msg", "该代理商不存在上级");jsonObject.put("error", "1");
		}else {
			//把邀请码设置到数据库
			agency.setInvitecode(invitecode);
			//根据邀请码查询上级信息,把查到的上级id设置到数据库
			Agency agency2=agencyService.selectOne(new EntityWrapper<Agency>().eq("loginname", invitecode));
			agency.setPid(agency2.getId()+"");
			//查询上上级信息,把查到的上上级id设置到数据库
			agency.setPiddetails(agency2.getPiddetails()+","+agency.getId());
			if (!agencyService.updateById(agency)) {
				jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
				
			}
		
		}
		jsonObject.put("msg", "成功"); 
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}

	//代理商登录
	@RequestMapping(value = "/app/agency/login")
	@ResponseBody
	public String app_agency_login(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		//获取参数
		String loginname="";
		String loginpass="";
		//验证参数
		if (UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "账号格式错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getStringPass(request.getParameter("loginpass"))){loginpass = request.getParameter("loginpass").trim();}else{jsonObject.put("msg", "密码格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		//通过账号查询此条数据
		Agency agency=agencyService.selectOne(new EntityWrapper<Agency>().eq("loginname", loginname));
		//判断此账号是否有数据
		if(agency==null){//如果没有数据 那说明不存在此账号
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//走到这里说明有账号，那么要判断这个账号的密码是否正确，传过来的密码要加密后再对比
		if(!agency.getLoginpass().equals(MD5Hash.getMd5HashPasswod(loginpass))){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//生成一个随机数当作token
		agency.setToken(UtsUuid.getUuid());
		//把这个token更新到数据库，每次登陆都要重新生成一次
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//把实体类里面的密码清空 然后返回给前台(所有返回代理商信息的时候都要清空重要的信息，比如密码，身份证，等等)
		agency.setLoginpass("");
		jsonObject.put("data", agency);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//获取代理商token信息
	@RequestMapping(value = "/app/agency/token")
	@ResponseBody
	public String app_agency_token(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		String token="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Agency agency=agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "账号或者密码错误");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		agency.setLoginpass("");
		jsonObject.put("data", agency);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
		
	}
	
	//代理商密码修改
	@RequestMapping("/app/agency/pass/up")
	@ResponseBody
	public String app_agency_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String ypass = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("ypass"))){ypass=request.getParameter("ypass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "原密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		Agency agency = agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "请重新登陆");return jsonObject.toString();
		}
		
		if(!MD5Hash.getMd5HashPasswod(ypass).equals(agency.getLoginpass())){
			jsonObject.put("error", "1");jsonObject.put("msg", "原始密码 错误");return jsonObject.toString();
		}
		
		agency.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!agencyService.updateById(agency)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	//代理商头像修改
	@RequestMapping("/app/agency/touxiang/up")
	@ResponseBody
	public String app_agency_touxiang_up(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String img = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setImg(img);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商性别修改
	@RequestMapping("/app/agency/sex/up")
	@ResponseBody
	public String app_agency_sex_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String sex = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		if ("男".equals(sex)) {
			agency.setSex(sex);
		}else if("女".equals(sex)){
			agency.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商年龄修改
	@RequestMapping("/app/agency/age/up")
	@ResponseBody
	public String app_agency_age_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String age = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setAge(age);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商昵称修改
	@RequestMapping("/app/agency/nicheng/up")
	@ResponseBody
	public String app_agency_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String nicheng = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setNicheng(nicheng);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商名字修改
	@RequestMapping("/app/agency/name/up")
	@ResponseBody
	public String app_agency_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String name = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setName(name);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商邮箱修改
	@RequestMapping("/app/agency/email/up")
	@ResponseBody
	public String app_agency_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String email = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Agency agency =agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg", "请重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		agency.setEmail(email);
		if(!agencyService.updateById(agency)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//代理商密码找回
	@RequestMapping(value={"/app/agency/psw/find"})
	@ResponseBody
	public String app_agency_psw_find(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject();
		String loginname="";
		String nloginpass="";
		String nloginpass2="";
		if(UtsReg.getIntXl(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "代理商名 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("nloginpass"))){nloginpass=request.getParameter("nloginpass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("nloginpass2"))){nloginpass2=request.getParameter("nloginpass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		
		Agency agency = agencyService.selectOne(new EntityWrapper<Agency>().eq("loginname", loginname));
		if(agency==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "该代理商不存在");return jsonObject.toString();
		}
		if(!nloginpass.equals(nloginpass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		
		
		agency.setLoginpass(MD5Hash.getMd5HashPasswod(nloginpass2));
		
		if(!agencyService.updateById(agency)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
}
